package com.whut.googleplay.fragment;

import android.view.View;
import android.widget.ListView;

import com.whut.googleplay.adapter.BaseListAdapter;
import com.whut.googleplay.bean.AppInfo;
import com.whut.googleplay.protocol.HomeProtocol;
import com.whut.googleplay.utils.UIUtils;
import com.whut.googleplay.widget.LoadingPage;

import java.util.List;

/**
 * ============================================================
 * 作 者 :        xyb
 * 版 本 :        1.0
 * 创建日期 ：     2016-04-20 下午2:34
 * 描 述 ：
 * 修订历史 ：
 * ============================================================
 **/
public class HomeFragment extends BaseFragment {

    private ListView mListView ;

    private List<AppInfo> mDataList;


    @Override
    protected LoadingPage.LoadResult load() {
        HomeProtocol homeProtocol = new HomeProtocol();
        mDataList = homeProtocol.load(0);
        return check(mDataList);
    }

    public LoadingPage.LoadResult check(Object mList) {

        if(mList == null){
            return LoadingPage.LoadResult.ERROR;
        }

        if(mList instanceof List){
            List list = (List) mList;
            if(list.size() == 0){
                return LoadingPage.LoadResult.EMPTY;
            }
        }

        return LoadingPage.LoadResult.SUCCESS;
    }

    @Override
    protected View createSuccessView() {
        mListView = new ListView(UIUtils.getContext());

        mListView.setAdapter(new HomeAdapter(mListView,mDataList));

        return mListView;
    }

    public class HomeAdapter extends BaseListAdapter{

        public HomeAdapter(ListView listView, List mDataList) {
            super(listView, mDataList);
        }


        @Override
        protected List onLoadMore() {
            HomeProtocol protocol = new HomeProtocol();
            return protocol.load(getData().size());
        }


    }
}
